package com.ch.home.controller.userController;

import com.ch.home.bean.Feedback;
import com.ch.home.bean.User;
import com.ch.home.service.serviceImpl.userServiceImpl.FeedbackServiceImpl;
import com.ch.home.service.serviceImpl.userServiceImpl.UserServiceImpl;
import com.ch.home.utils.SnowFlake;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;

/**
 * 个人信息界面操作
 * Created by CHEN on 2019/8/14.
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserServiceImpl userService;

    @Autowired
    FeedbackServiceImpl feedbackService;

    SnowFlake snowFlake = new SnowFlake(0, 1);

    /**
     * 返回用户名
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public String getUserName(@PathVariable("id") Long id) {
        return userService.getUserName(id);
    }


    @CrossOrigin
    @PostMapping(value = "/login")
    public HashMap login(
            @RequestParam(required = false) String username,
            @RequestParam(required = false) String password,
            @RequestParam(required = false) String phone,
            @RequestParam(required = false) String QQ) {

        User user = new User(username, password, phone, QQ);
        System.out.println("-------------------------------------------");
        System.out.println(user);
        System.out.println("-------------------------------------------");

        HashMap<String, Object> result = userService.login(user);
        return result;
    }

    /**
     * 用户注册功能
     * url:http://192.168.43.158:8082/user/register
     * 传参示例：{}
     *
     * @param
     * @return HashMap
     */
    @PostMapping(value = "/register")
    public HashMap register(@RequestParam(required = false) Long userId,
                            @RequestParam(required = false) String username,
                            @RequestParam(required = false) String password,
                            @RequestParam(required = false) Integer age,
                            @RequestParam(required = false) Integer gender,
                            @RequestParam(required = false) String phone,
                            @RequestParam(required = false) String QQ,
                            @RequestParam(required = false) String email,
                            @RequestParam(required = false) String portrait,
                            @RequestParam(required = false) Integer authority,
                            @RequestParam(required = false) Integer status,
                            @RequestParam(required = false) Long integral) {
        HashMap result;


        User user = new User(userId, username, password, age, gender, phone, QQ, email, portrait, authority, status, integral, 0, 0);
        System.out.println(user);
        boolean exists = userService.judgeExists(user);
        result = userService.register(user, exists);
        return result;

    }

    /**
     * 用户其他方式注册：手机QQ
     *
     * @param
     * @return HashMap
     */
    @PostMapping(value = "/otherRegister")
    public HashMap otherRegister(@RequestParam(required = false) Long userId,
                                 @RequestParam(required = false) String username,
                                 @RequestParam(required = false) String password,
                                 @RequestParam(required = false) Integer age,
                                 @RequestParam(required = false) Integer gender,
                                 @RequestParam(required = false) String phone,
                                 @RequestParam(required = false) String QQ,
                                 @RequestParam(required = false) String email,
                                 @RequestParam(required = false) String portrait,
                                 @RequestParam(required = false) Integer authority,
                                 @RequestParam(required = false) Integer status,
                                 @RequestParam(required = false) Long integral,
                                 @RequestParam(required = false) Integer followNum,
                                 @RequestParam(required = false) Integer fansNum) {
        HashMap result;
        User user = new User(userId, username, password, age, gender, phone, QQ, email, portrait, authority, 1, Long.valueOf(0), 0, 0);
//        User user = new User(null, "23青年", "password", 11, 1, "12312312311", QQ, email, portrait, authority, 1, Long.valueOf(0), 0, 0);
        System.out.println(user);
        boolean exists = userService.judgeExists(user);

        result = userService.otherRegister(user, exists);
        return result;
    }


    /**
     * 用户信息更新
     * 包括：个人信息更新，状态更新，积分更新
     * 关注于点赞功能暂未开启
     * 小注意：此处用户重名还没写
     *
     * @return
     */
    @PostMapping(value = "/info/update")
    public HashMap update(@RequestParam(required = false) Long userId,
                          @RequestParam(required = false) String username,
                          @RequestParam(required = false) String password,
                          @RequestParam(required = false) Integer age,
                          @RequestParam(required = false) Integer gender,
                          @RequestParam(required = false) String phone,
                          @RequestParam(required = false) String QQ,
                          @RequestParam(required = false) String email,
                          @RequestParam(required = false) String portrait,
                          @RequestParam(required = false) Integer authority,
                          @RequestParam(required = false) Integer status,
                          @RequestParam(required = false) Long integral,
                          @RequestParam(required = false) Integer collectNum) {

        User user = new User(userId, username, password, age, gender, phone, QQ, email, portrait, authority, status, integral, collectNum);
        System.out.println(user);
        HashMap result = userService.update(user);
        return result;
    }

    /**
     * 用户信息更新
     * 包括：个人信息更新，状态更新，积分更新
     * 关注于点赞功能暂未开启
     * 小注意：此处用户重名还没写
     *
     * @return
     */
    @PostMapping(value = "/info/get")
    public HashMap getUserInfo(@RequestParam(required = false) Long userId,
                               @RequestParam(required = false) String username,
                               @RequestParam(required = false) String phone,
                               @RequestParam(required = false) String QQ,
                               @RequestParam(required = false) String email) {

        User user = new User(userId, username, null, null, null, phone, QQ, email, null, null, null, null, 0);
        System.out.println(user);
        HashMap result = userService.getUserByCondition(user);
        return result;
    }


    /**
     * 列出所有用户信息
     *
     * @return
     */
    @CrossOrigin
    @PostMapping("/list")
    public LinkedList<User> listUser() {
        return userService.listUser( );
    }

    /**
     * 意见反馈（未测试）
     *
     * @param userId
     * @param content
     * @return
     */
    @PostMapping(value = "/feedback/add")
    public HashMap addFeedback(@RequestParam(required = false) Integer userId,
                               @RequestParam(required = false) String content) {

        HashMap<Object, Object> result;
        Feedback feedback = new Feedback(null, userId, content, new Date( ));

        result = feedbackService.addFeedback(feedback);

        return result;
    }


    /**
     * 根据条件查询反馈（未测试）
     * @param userId
     * @param earlierTime
     * @param laterTime
     * @return
     */
    @PostMapping(value = "/feedback/get")
    public HashMap getFeedbackByCondition(@RequestParam(required = false) Integer userId,
                                          @RequestParam(required = false) Date earlierTime,
                                          @RequestParam(required = false) Date laterTime) {
        HashMap<Object, Object> result;
        result = feedbackService.getFeedback(userId, earlierTime, laterTime);
        return result;
    }




}